package system

import (
	"fmt"
	"github.com/bwmarrin/snowflake"
	"os"
	"path/filepath"
)

const DirName = "fileContainer"

var path, _ = os.Getwd()
var DirContainer = filepath.Join(path, DirName)

// GetFilePath 获取文件路径
func GetFilePath(suffix string) (string, error) {
	_, err := os.Stat(DirContainer)
	if os.IsNotExist(err) {
		err := os.MkdirAll(DirContainer, os.ModeDir)
		if err != nil {
			return "", fmt.Errorf("创建目录失败：%w", err)
		}

	}
	node, _ := snowflake.NewNode(1)
	id := node.Generate()
	return filepath.Join(DirContainer, fmt.Sprintf("%v.%v", id, suffix)), nil
}

func WriteFile(filePath string, content []byte) error {
	err := os.WriteFile(filePath, content, 0644)
	if err != nil {
		return fmt.Errorf("写入文件失败：%w", err)
	}
	return nil
}

func ReadFile(filePath string) ([]byte, error) {
	content, err := os.ReadFile(filePath)
	if err != nil {
		return nil, fmt.Errorf("读取文件失败：%w", err)
	}
	return content, nil
}
